﻿using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace Magic.Web.Entry;

/// <summary>
/// 角色组服务
/// </summary>
public class SysRoleController : BaseController
{
    private readonly ISysRoleService _service;
    private readonly ISysUserRoleService _accessService;
    public SysRoleController(ISysRoleService service, ISysUserRoleService accessService)
    {

        _service = service;
        _accessService = accessService;
    }
    /// <summary>
    /// 获取角色分组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("getIndex")]
    public async Task<dynamic> GetIndex([FromBody] SysRolePageInput input)
    {
        if (!input.IsTree)
        {
            var list = await _service.GetTree(input);
            var group = await _accessService.GetListByUserId(UserManager.UserId);
            return new
            {
                group,
                list
            };
        }
        else
        {
            return await _service.GetPageList(input);
        }

    }
    /// <summary>
    /// 根据Id返回分组
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("getDetail")]
    public async Task<SysRoleDetailOutput> GetDetail([Required(ErrorMessage = "用户Id不能为空")][FromQuery] int id)
    {
        return await _service.Get(id);
    }
    /// <summary>
    /// 添加分组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("add")]
    public async Task Add(AddSysRoleInput input)
    {
        await _service.Add(input);
    }
    /// <summary>
    /// 删除分组
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task Delete([FromQuery] string ids)
    {
        await _service.Delete(ids.SplitToIntList().ToList());
    }
    /// <summary>
    /// 编辑分组
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("edit")]
    public async Task Edit(EditSysRoleInput input)
    {
        await _service.Edit(input);
    }
}